package com.blast.manager.blast.presenter

import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.View
import androidx.core.content.ContextCompat
import com.blast.manager.R
import com.blast.manager.blast.BlastActivity
import com.blast.manager.net.presenter.AbsNetCheckPresenter
import com.blast.manager.net.presenter.NetCheckChargePresenter
import com.wxl.common.base.AbsActivity
import com.wxl.common.life.ViewModelQuick
import com.wxl.common.room.entity.BlastRecordEntity
import com.wxl.common.room.entity.LeiGuanEntity
import com.wxl.common.util.Loog
import com.wxl.common.wiget.ProgressUpdatePop
import kotlinx.android.synthetic.main.layout_blast_count_down.view.*


/**
 * 起爆倒计时
 */
class BlastCountDownPresenter : AbsNetCheckPresenter() {

    var count = 5
    var isShowPop = false
    var mHandler: Handler? = null
    val COUNT_DOWN_WHAT = 999
    var isStopCountDown = false

    override fun showView(vararg args: Any) {
        rootView.blastCountDownTextBar.start()
        rootView.blastCountDownTextBar.timeMillis = 60000
        rootView.blastCountDownTextBar.setCountdownProgressListener(1) { what, progress ->
            if (what == 1) {
                val t = 60 * progress / 100
                rootView.blastCountDownTextBar.text = t.toString()
                if (0 == t) {
                    (context as BlastActivity).showWarn()
                }
            }
        }

        if (mHandler == null) {
            mHandler = object : Handler(Looper.getMainLooper()) {
                override fun handleMessage(msg: Message) {
                    if (msg.what == COUNT_DOWN_WHAT && !isStopCountDown) {
                        count--
                        toBlast()
                    }
                }
            }
        }
    }

    override fun getLayoutResId(): Int {
        return R.layout.layout_blast_count_down
    }


    override fun onDestroy() {
        super.onDestroy()
        if (isAdd()) {
            stopCountDown()
            mHandler = null
        }
    }


    /**
     * 正常模式
     */
    fun blastNormalModel(){
        isShowPop = false
        isStopCountDown = false
        count = 5
        rootView.blastCountDownTextBar.text = "5"
        rootView.blastCountDownTextBar.timeMillis = 5000
        rootView.blastCountDownTextBar.reStart()
        rootView.blastCountDownTextBar.setCountdownProgressListener(
            4
        ) { what, progress ->
            val t = 5 * progress / 100
            rootView.blastCountDownTextBar.text = t.toString()
        }
        toBlast()
    }


    /**
     * 开始爆破
     */
    fun startBlast() {
        isShowPop = false
        isStopCountDown = false
        count = 3
        rootView.blastCountDownTextBar.text = "3"
        rootView.blastCountDownTextBar.timeMillis = 3000
        rootView.blastCountDownTextBar.reStart()
        rootView.blastCountDownTextBar.setCountdownProgressListener(
            2
        ) { what, progress ->
            val t = 3 * progress / 100
            rootView.blastCountDownTextBar.text = t.toString()
        }
        toBlast()
    }

    /**
     * 测试1起爆模式
     */
    fun startTestModel1Blast() {
        count = 10
        rootView.blastCountDownTextBar.text = "10"
        rootView.blastCountDownTextBar.timeMillis = 10000
        rootView.blastCountDownTextBar.reStart()
        rootView.blastCountDownTextBar.setCountdownProgressListener(3)
        { what, progress ->
            val t = 10 * progress / 100
            rootView.blastCountDownTextBar.text = t.toString()
            if (progress == 0) {
                (context as BlastActivity).showWarn()
            }
        }
        blast(3)
    }

    /**
     * 直接起爆
     */
    fun blast() {
        stopCountDown()
        count = 0
        toBlast()
    }


    private fun toBlast() {
        if (count > 0) {
            mHandler?.sendEmptyMessageDelayed(COUNT_DOWN_WHAT, 1000)
        }
        if (count == 0 && !isShowPop) {
            isShowPop = true
            rootView.blastCountDownTextBar.visibility = View.GONE
            rootView.blastCountDownText.text = "正在爆破..."
            (context as BlastActivity).dismissClearAllPop()
            ProgressUpdatePop.show(context)
        }

        if (count >= 0) {
            rootView.blastCountDownTextBar.text = count.toString()
            blast(count)
        }
    }


    fun stopCountDown() {
        isStopCountDown = true
        rootView.blastCountDownTextBar.stop()
        mHandler?.removeCallbacksAndMessages(-1)
        mHandler?.removeMessages(COUNT_DOWN_WHAT)
    }

    fun restartCountDown() {
        stopCountDown()
        show()
    }


    private fun blast(count: Int) {
        Loog.e("blast count:$count")
        (context as BlastActivity).addLog("起爆", 0, "开始起爆")
        ViewModelQuick.get((context as AbsActivity), BlastSerialPresenter::class.java)
            .blast(count,
                object : BlastSerialPresenter.OnBlastCallback {
                    override fun onBlastResult(blastRecords: List<BlastRecordEntity>) {
                        ProgressUpdatePop.updateProgress(100, "爆破完成...", "正在爆破", 100)
                        rootView.blastCountDownText.setTextColor(
                            ContextCompat.getColor(
                                context,
                                android.R.color.holo_green_dark
                            )
                        )
                        rootView.blastCountDownText.text = "爆破成功!"
                        (context as AbsActivity).dismissClearAllPop()
                        (context as BlastActivity).showBlastResult(blastRecords)
                    }

                    override fun onBlastProgress(p: Int, sub: String, max: Int) {
                        ProgressUpdatePop.updateProgress(p, sub, "正在爆破", max)
                    }

                    override fun onBlastBlockLockProgress(p: Int, sub: String, max: Int) {
                        ProgressUpdatePop.updateBlockLockProgress(p, sub, "正在爆破", max)
                    }

                    override fun onVoltageElectricChanged(mVoltage: Int, mElectric: Int) {
                        ViewModelQuick.get(
                            (context as BlastActivity),
                            NetCheckChargePresenter::class.java
                        ).onChargeElectricChanged(mVoltage, mElectric)
                    }

                    override fun onBlastProgressError(
                        e: String,
                        blastError: List<LeiGuanEntity>
                    ) {
                        ProgressUpdatePop.updateProgress(100, e, "正在爆破", 100)
                        (context as AbsActivity).dismissClearAllPop()
                        rootView.blastCountDownText.text = e
                        (context as BlastActivity).showBlastErrorResult(blastError)
                    }
                })

    }

}